<template>
    <div>
        <div @click="popup.openPopup" class="vue-flow-model">
            <n-icon size="30" color="#18A058">
                <data-share/>
            </n-icon>
            <Handle id="rowColumnConversion-inlet" type="target" :position="Position.Left" :is-valid-connection="props.isValidSourcePos"/>
            <Handle id="rowColumnConversion-export" type="source" :position="Position.Right" :is-valid-connection="props.isValidTargetPos"/>
        </div>
        <span>行列转换</span>
        <flow-popup-window
            :show="popup.show"
            :on-click-outside="popup.clickPopupOutside"
            :x="popup.x"
            :y="popup.y"
        >
            <n-card title="行列转换">
                <template #footer>
                    <n-space>
                        <n-button @click="getSuperiorData">获取上级节点数据</n-button>
                        <n-button @click="rowColumnConversion">行列转换</n-button>
                    </n-space>
                </template>
            </n-card>
        </flow-popup-window>
    </div>
</template>

<script setup>

import {Handle, Position} from "@vue-flow/core";
import {DataShare} from "@vicons/carbon";
import {useElementsStore} from "@/stores/elements";
import workApi from "@/http/workApi";
import FlowPopupWindow from "@/components/VueFlowModel/vueFlowPopupWindow/FlowPopupWindow.vue";
const props = defineProps(['isValidTargetPos', 'isValidSourcePos','id','data'])
const elements = useElementsStore()
// 点击弹窗
const popup = reactive({
    show: false,
    x: null,
    y: null,
    wait: false,
    clickPopupOutside: () => {
        popup.closedPopup()
    },
    openPopup: (e) => {
        popup.x = e.x
        popup.y = e.y
        popup.show = true
    },
    closedPopup: () => {
        if (!popup.wait) {
            popup.show = false;
        }
    },
    reopen: () => {
        popup.show = true
    }
})

const getSuperiorData = ()=>{
    // 获取上级节点数据
    for (let i of elements.elements) {
        if (i['target']===props.id) {
            for (let j of elements.elements) {
                if (j.id === i['source']) {
                    props.data.data = {...j.data.data}
                    props.data.data.title.map(s=>{
                        // 移除可修改
                        delete s.render
                        return s
                    })
                    return
                }
            }
        }
    }
}
const rowColumnConversion = ()=>{
    if (!("data" in props.data.data)) {
        window.$message.warning("当前节点没有数据, 需要连线并获取上级节点数据")
        return
    }
    // 发送数据到后台进行行列转换
    workApi.post("perform_tasks/row_column",{
        data:props.data.data
    }).then(s=>{
        props.data.data = s.data
    });
}
// 单页数据量
const pagination = {
    pageSize: 15
}
</script>

<style scoped lang="less">

</style>